package com.hup.utils.selenium.constant;

import com.hup.utils.commons.annotation.BaseConstantParam;
import com.hup.utils.commons.interfaces.BaseConstant;
import lombok.Getter;
import lombok.extern.log4j.Log4j2;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.ie.InternetExplorerDriverService;

import java.util.function.Function;

/**
 * @author hugan
 * @date 2020/3/13
 */
@Log4j2
public class BrowserConstant {

    @BaseConstantParam(desc = "浏览器标志")
    public enum BrowserType implements BaseConstant {
        CHROME("chrome", ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, headless -> {
            /*
            这里创建的浏览器,在driver.get()时是一直阻塞的!期间不能通过发送js或按键指令打断加载!(指令要等到get完后才能发送成功)
            https://blog.csdn.net/yexiaomodemo/article/details/99958509
            options.setPageLoadStrategy(PageLoadStrategy.NONE);
             */
            if (!headless) {
                return new ChromeDriver();
            } else {
                ChromeOptions options = new ChromeOptions();
                options.setHeadless(true);
                return new ChromeDriver(options);
            }
        }),
        IE("ie_未完成", InternetExplorerDriverService.IE_DRIVER_EXE_PROPERTY, null);

        /**
         * 配置文件中浏览器标志的值
         */
        @Getter
        public final String desc;
        public final String driverKey;
        /**
         * 浏览器初始化对象,用于启动浏览器;
         * 入参[boolean:true-无头浏览器]
         */
        public final Function<Boolean, WebDriver> starter;

        BrowserType(String desc, String driverKey, Function<Boolean, WebDriver> starter) {
            this.desc = desc;
            this.driverKey = driverKey;
            this.starter = starter;
        }

    }

    /**
     * 浏览器生命周期状态
     */
    public enum BrowserLifeState {
        /**
         * 可用的,闲置的
         */
        ACCESSIBLE("闲置的"),
        STARTING("启动中"),
        CLOSING("关闭中"),
        SETTING("设置中"),
        ;

        public final String desc;

        BrowserLifeState(String desc) {
            this.desc = desc;
        }
    }

}
